package com.javashow.core;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.SessionFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

@SuppressWarnings("unchecked")
public class BaseDao{
	/*public static Log log = LogFactory.getLog(BaseDao.class);

	//1--由于使用spring的annotation注入时，HibernateDaoSupport不能注入sessionFactiry和hibernateTemplemet
	//2-由于HibernateDaoSupport的setSession方法为final，子类不能重写
	@Resource
	public void setSessionFactory0(SessionFactory sessionFactory){  
		super.setSessionFactory(sessionFactory);  
	}
	
	protected void initDao() {
		// do nothing
	}

	public void save(Object transientInstance) {
		log.debug("saving pojo instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Object persistentInstance) {
		log.debug("deleting pojo instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}
	
	public void deleteById(Class pojo , String id) {
		log.debug("deleting pojo instance");
		try {
			getHibernateTemplate().delete(findById(pojo, id));
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Object findById(Class pojo , String id) {
		log.debug("getting pojo instance with id: " + id);
		try {
			return getHibernateTemplate().get(pojo.getName(),new Long(id));
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Object instance) {
		log.debug("finding pojo instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String pojoName,String propertyName, Object value) {
		log.debug("finding pojo instance with property: " + propertyName+ ", value: " + value);
		try {
			String queryString = "from "+pojoName+" as model where model."+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List find(String HQL) {
		log.debug("finding pojo instances with hql");
		try {
			return getHibernateTemplate().find(HQL);
		} catch (RuntimeException re) {
			log.error("find failed", re);
			throw re;
		}
	}
	
	public List findAll(String pojoName) {
		log.debug("finding all pojo instances");
		try {
			String queryString = "from "+pojoName;
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Object merge(Object detachedInstance) {
		log.debug("merging pojo instance");
		try {
			Object result = getHibernateTemplate().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Object instance) {
		log.debug("attaching dirty pojo instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Object instance) {
		log.debug("attaching clean pojo instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static Object getFromApplicationContext(ApplicationContext ctx,String beanName) {
		return (Object) ctx.getBean("beanName");
	}*/
}
